package jp.tkym.labs.swing3d;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;

import jp.tkym.labs.m3d.Drawable;
import jp.tkym.labs.swing3d.drawer.DrawG2D;

/**
 * 
 * @author takayama
 */
public class CanvasSwing implements PaintableLayerComponent, Drawable<DrawG2D>{
	public static final String SIZE_PROPERTY = "size";
	private List<DrawG2D> afterList = new ArrayList<DrawG2D>();
	private Dimension size = new Dimension();
	private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
	private final JComponent component;
	
	public CanvasSwing(JComponent component){
		this.component = component;
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener){
		pcs.addPropertyChangeListener(listener);
	}
	
	@Override
	public void setSize(Dimension dim){
		if(!size.equals(dim))
			pcs.firePropertyChange(SIZE_PROPERTY, size, dim);
		size.height = dim.height;
		size.width = dim.width;
	}
	
	@Override
	public void paintAfter(Graphics g){
		for(DrawG2D d : afterList) 
			d.draw((Graphics2D)g);
	}
	
	@Override
	public void paintBefore(Graphics g){ /** do noting */ }
	
	@Override
	public List<DrawG2D> drawList() {
		return afterList;
	}
	
	@Override
	public void repaint(){
		component.repaint();
	}
}